package com.shengde.jinwawaforleader.model.myJob.classList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.network.BaseProtocol;

import java.util.ArrayList;

public class ChangeStuClass extends BaseActivity {
    private TextView tv_back;
    private TextView fill_add;
    private TextView tv_title;
    private String name;
    private ListView lv_changestu;
    private ArrayList<String> classlist;
    private ArrayList<Integer> allclassid;
    private CommonBaseAdapter<String> adapter;
    private int curclassid;
    private int studentid;
    private int positionOfClassId;

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_stu_class);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        classlist = intent.getStringArrayListExtra("classlist");
        allclassid = intent.getIntegerArrayListExtra("allclassid");
        curclassid = intent.getIntExtra("curclassid",0);
        studentid = intent.getIntExtra("studentid",0);
        positionOfClassId = allclassid.indexOf(curclassid);
    }

    @Override
    public void initView() {
        tv_back = (TextView)findViewById(R.id.tv_back);
        fill_add = (TextView)findViewById(R.id.fill_add);
        tv_title = (TextView)findViewById(R.id.tv_title);
        lv_changestu = (ListView)findViewById(R.id.lv_changestu);
    }

    @Override
    public void initData() {
        fill_add.setText(getString(R.string.baocun));
        tv_title.setText(name+getString(R.string.diaoban));

        adapter = new CommonBaseAdapter<String>(this,classlist,R.layout.item_changestu_class) {
            @Override
            public void convert(ViewHolder holder, int position) {
                TextView tv_class_name = holder.getView(R.id.tv_class_name);
                ImageView iv_gou = holder.getView(R.id.iv_gou);
                iv_gou.setVisibility(View.INVISIBLE);
                tv_class_name.setText(classlist.get(position));
                if(position == positionOfClassId) {
                    iv_gou.setVisibility(View.VISIBLE);
                }
            }
        };
        lv_changestu.setAdapter(adapter);
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);
        fill_add.setOnClickListener(this);

        lv_changestu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                positionOfClassId = position;
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.fill_add:
                BaseProtocol.getInstance().postStuChangeClass(this,studentid,allclassid.get(positionOfClassId));
                break;
        }
    }
}
